import { GCellRenderer } from "../../../../../com/mui/controls/GCellRenderer";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { Cache } from "../../../cache/Cache";
import { PetInfo } from "../../../resource/info/PetInfo";
import { PetExplorePetStatus } from "./PetExplorePetStatus";
import { TBoss } from "../../../../../Message/Db/Tables/TBoss";
import { AvatarInfo } from "../../../resource/info/AvatarInfo";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { Language } from "../../../../../extend/language/Language";
import { SPetExploreInFight } from "../../../../../Message/Public/SPetExploreInFight";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { DataEvent } from "../../../events/DataEvent";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
type int = number;
//class PetExploreEntityItem
    
    export  class PetExploreEntityItem extends GCellRenderer
    {
        constructor()
        {
            super();
            this.init();
            this.setSize(30, 30);
            NetDispatcher.addCmdListener(ServerCommand.PetExploreFightIndexCommand, this.onPetExploreFightIndexCommandHandler);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            this._info = arg1["info"];
            this._index = arg1["index"] as int;
            if (typeof this._info === "string") 
            {
                this._uid = this._info as string;
                this._petInfo = Cache.instance.pet.getPetInfoByUid(this._uid);
                this._entityName = this._petInfo.name;
                this.loadAvatar(PetInfo.getPetAvatarUrl(this._petInfo.changeCode));
                if (this._index == 0) 
                {
                    this.status = PetExplorePetStatus.Fighting;
                }
                this._isPet = true;
            }
            else if (typeof this._info === "tboss") 
            {
                this._bossInfo = this._info as TBoss;
                this._entityName = this._bossInfo.name;
                this.loadAvatar(AvatarInfo.getBossAvatarName(this._bossInfo.code));
                this._isPet = false;
            }
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(0, 0, 30, 30, this, ImagesConst.PackItemBg);
            this._avatar = new Object /* flash.display.Bitmap */();
            this._avatar.x = 2;
            this._avatar.y = 2;
            this.addChild(this._avatar);
            return;
        }

        /* internal  */loadAvatar(arg1: string): void
        {
            var imgName: string;

            var loc1;
            imgName = arg1;
            LoaderManager.instance.load(imgName, (arg1: ImageInfo): void
            {
                _avatar.bitmapData = arg1.bitmapData;
                _avatar.width = 24;
                _avatar.height = 24;
                return;
            })
            return;
        }

        public get status(): int
        {
            return this._status;
        }

        public set status(arg1: int)
        {
            this._status = arg1;
            if (this._status != PetExplorePetStatus.Fighting) 
            {
                if (this._status != PetExplorePetStatus.Wait) 
                {
                    if (this._status == PetExplorePetStatus.Death) 
                    {
                        this._avatar.filters = [FilterConst.colorFilter];
                        this._statusStr = Language.getString(54470);
                    }
                }
                else 
                {
                    this._avatar.filters = [];
                    this._statusStr = Language.getString(54469);
                }
            }
            else 
            {
                this._avatar.filters = [FilterConst.worldMapFilter];
                this._statusStr = Language.getString(54468);
            }
            this.toolTipData = "<textFormat leading=\'5\'><font color=\'#00ff00\' size=\'16\'>" + this._entityName + "</font>\n<font color=\'#ffffff\'>" + this._statusStr + "</font></textFormat>";
            return;
        }

        /* internal  */onPetExploreFightIndexCommandHandler(arg1: SPetExploreInFight): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            var loc4=false;
            if (arg1) 
            {
                loc1 = arg1.index;
                if (loc3 = Cache.instance.petExplore.battlePetEntityInfo) 
                {
                    loc2 = loc3.entityId;
                    if (loc2 && EntityUtil.equal(arg1.entityId, loc2)) 
                    {
                        loc4 = true;
                    }
                }
                if (loc4 && this._isPet || !loc4 && !this._isPet) 
                {
                    this.updateStatus(loc1);
                }
            }
            return;
        }

        /* internal  */onPetExploreEntityUpdateHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc1=arg1.data["isPet"] as Boolean;
            if (this._isPet == false && loc1 == false) 
            {
                loc3 = Cache.instance.petExplore.battleBossEntityInfo.entityId;
                loc4 = Cache.instance.petExplore.selectedConfigInfo;
                loc2 = ThingUtil.sceneEntityUtil.getEntity(loc3, Cache.instance.petExplore.selectedConfigInfo.map);
                if (loc2) 
                {
                    loc5 = loc4.getBossIndexById(loc2.entityInfo.entityInfo.code);
                }
                if ((loc6 = loc4.getBossIndexById(this._bossInfo.code)) < loc5) 
                {
                    this.status = PetExplorePetStatus.Death;
                }
                else if (loc6 != loc5) 
                {
                    this.status = PetExplorePetStatus.Wait;
                }
                else 
                {
                    this.status = PetExplorePetStatus.Fighting;
                }
            }
            return;
        }

        /* internal  */updateStatus(arg1: int): void
        {
            var loc1=arg1;
            if (this._index < loc1) 
            {
                this.status = PetExplorePetStatus.Death;
            }
            else if (this._index != loc1) 
            {
                this.status = PetExplorePetStatus.Wait;
            }
            else 
            {
                this.status = PetExplorePetStatus.Fighting;
            }
            return;
        }

        /* internal */ /* var */_avatar: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_name: string="";

        /* internal */ /* var */_status: int;

        /* internal */ /* var */_statusStr: string="";

        /* internal */ /* var */_tip: string;

        /* internal */ /* var */_uid: string;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_entityName: string;

        /* internal */ /* var */_bossInfo: TBoss;

        /* internal */ /* var */_isPet: Boolean;

        /* internal */ /* var */_index: int;

        /* internal */ /* var */_info: Object;
    }
